﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMLogging;
using System.Windows.Media.Imaging;
using System.IO;

namespace GMUtilitiesDotNet.Helpers
{
    public static class IOHelpers
    {
        #region Methods
        public static Uri GetUriWithChecks(string pAbsoluteUriPath)
        {
            Uri uri = null;
            try
            {
                uri = new Uri(pAbsoluteUriPath, UriKind.Absolute);
            }
            catch (ArgumentNullException e)
            {
                Logger.Error(e.Message);
                return null;
            }
            catch (ArgumentException e)
            {
                Logger.Error(e.Message);
                return null;
            }
            catch (UriFormatException e)
            {
                Logger.Error(e.Message);
                return null;
            }

            return uri;
        }

        public static BitmapImage GetBitmapImageWithChecks(Uri pUri)
        {
            BitmapImage bitmapImage = null;
            try
            {
                bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.UriSource = pUri;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
            }
            catch (InvalidOperationException e)
            {
                Logger.Error(e.Message);
                return null;
            }
            catch (FileNotFoundException e)
            {
                Logger.Error(e.Message);
                return null;
            }
            catch (NotSupportedException e)
            {
                Logger.Error(e.Message);
                return null;
            }

            return bitmapImage;
        }
        #endregion
    }
}